package com.aaa.inter;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginIntegr implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
      // 到达controller之前  判断一下session里面是否有值  有  true
        HttpSession session = request.getSession();
        Object name = session.getAttribute("name");
        if(name==null){
            // /项目名字/views/login.jsp
            // 跳转到登录页面  重定向
            String contextPath = request.getContextPath();
            String path=contextPath+"/views/login.jsp";
            response.sendRedirect(path);
            return false;
        }else{
            // 如果有下一个拦截器  controller

            return true;
        }


        //return false;
    }


    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
